﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 账户五日均值
    /// </summary>
    public class ServiceAccountFiveDayAvg : Benefit.Interface.Day.IAccountFiveDayAvg
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountFiveDayAvg(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除五日的均值数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountFiveDayAvg where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountFiveDayAvg d in query)
            {
                db.AccountFiveDayAvg.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号五日均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountFiveDayAvg(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            List<Models.Sys.TradeHistory> ths = ith.GetFrontFiveDayTradeHistoryIds(tradeHistoryId);
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
                Models.Day.AccountFiveDayAvg change = new Models.Day.AccountFiveDayAvg();
       
              //  Sys.Account _account = new Sys.Account();
                change.AccountId = iacount.GetAccountId(account.LoginAccount);
                List<Models.Day.AccountDayChange> _change = new List<Models.Day.AccountDayChange>();
                foreach (Models.Sys.TradeHistory h in ths)
                {
                    var _query = from t in db.AccountDayChange where t.AccountId == change.AccountId && t.TradeHistoryId == h.Id select t;
                    if (_query.Count() > 0)
                    {
                        _change.Add(_query.First());
                    }
                }

                List<Models.Day.AccountDayAnalysis> _change2 = new List<Models.Day.AccountDayAnalysis>();
                foreach (Models.Sys.TradeHistory h in ths)
                {
                    var _query = from t in db.AccountDayAnalysis where t.AccountId == change.AccountId && t.TradeHistoryId == h.Id select t;
                    if (_query.Count() > 0)
                    {
                        _change2.Add(_query.First());
                    }
                }
                if (_change.Count() > 0)
                {
                    change.BillCount = Math.Round(_change.Average(a => a.BillCount), 2);
                    change.DayCount = Math.Round(_change.Average(a => a.DayCount), 2);
                    change.Free = Math.Round(_change.Average(a => a.Free), 2);
                    change.LoseCount = Convert.ToInt32(_change.Average(a => a.LoseCount));
                    change.Profit = Math.Round(_change.Average(a => a.Profit), 2);
                    change.SumLost = Math.Round(_change.Average(a => a.SumLost), 2);
                    change.SumWin = Math.Round(_change.Average(a => a.SumWin), 2);
                    change.UsedMargin = Math.Round(_change.Average(a => a.UsedMargin), 2);
                    change.WinCount = Convert.ToInt32(_change.Average(a => a.WinCount));
                }
                if (_change2.Count() > 0)
                {
                    change.AvgBillBenefit = Math.Round(_change2.Average(a => a.AvgBillBenefit), 2);
                    change.AvgPostionTime = Math.Round(_change2.Average(a => a.AvgPostionTime), 2);
                    change.MaxReturn = Math.Round(_change2.Average(a => a.MaxReturn), 2);
                    change.OverStopLoss = Math.Round(_change2.Average(a => a.OverStopLoss), 2);
                    change.PingJinPing = Convert.ToInt32(_change2.Average(a => a.PingJinPing));
                    change.RaiseRate = Math.Round(_change2.Average(a => a.RaiseRate), 2);
                    change.RiskCount = Convert.ToInt32(_change2.Average(a => a.RiskCount));
                    change.Utilization = Math.Round(_change2.Average(a => a.Utilization), 2);
                    change.WinRate = Math.Round(_change2.Average(a => a.WinRate), 2);
                    change.Yield = Math.Round(_change2.Average(a => a.Yield), 2);
                }
                change.TradeHistoryId = tradeHistoryId;
                db.AccountFiveDayAvg.Add(change);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 获取账号五日均值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public Models.Day.AccountFiveDayAvg GetAccountFiveDayAvg(int accountid, int tradeHistoryId)
        {
            Models.Day.AccountFiveDayAvg count = new Models.Day.AccountFiveDayAvg();
            var query = db.AccountFiveDayAvg.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }
    }
}